#include <stdlib.h>
#include "pt_list.h"

#define SOS_PT_LIST_SUCCESS (0)
#define SOS_PT_LIST_FAIL    (-1)

int sos_pt_list_insert(seL4_Word paddr, seL4_CPtr cap, sos_pt_list **list) {
    if (!list) return SOS_PT_LIST_FAIL;
    sos_pt_list *node = malloc(sizeof(struct sos_pt_list));
    node->paddr = paddr;
    node->cap = cap;
    DL_APPEND((*list), node);
    return SOS_PT_LIST_SUCCESS;
}

int sos_pt_list_destroy(sos_pt_list **list) {
    if (!list) return SOS_PT_LIST_FAIL;
    int err = 0;
    sos_pt_list *node, *tmp;

    DL_FOREACH_SAFE((*list), node, tmp) {
        // err |= seL4_ARM_PageTable_Unmap(node->cap);
        err |= cspace_delete_cap(cur_cspace, node->cap);
        ut_free(node->paddr, seL4_PageTableBits);
        DL_DELETE((*list), node);
        free(node);
    };

    return err;
}
